<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./js/vue.js"></script>
    <style>
        * {
            margin: 0;
            padding: 0;
            list-style: none;
        }

        .success {
            color: green
        }

        .error {
            color: red
        }
    </style>
</head>

<body>
    <div id="app">
        <div>
            用户名 <input v-model="username" />
            <span :class="usernameclass">{{usernametip}}</span>
        </div>
        <div>
            密码 <input v-model="userpwd" />
            <span :class="userpwdclass">
                {{userpwdtip}}
            </span>
        </div>
        <div>
            手机号 <input v-model="usertel" />
            <span :class="usertelclass">
                {{userteltip}}
            </span>
        </div>
    </div>

</body>
<script>
    new Vue({
        el: "#app",
        data: {
            username: "",
            usernamereg: /^[a-z_$][0-9a-z_$]{5,9}$/i,
            usernametip: "请输出数字字母_$组成并且不能以数字开头长度6到10位",
            usernameclass: "error",

            userpwd: "",
            userpwdreg: /^[0-9a-z]{6,10}$/i,
            userpwdtip: "密码数字字母组成长度6到10位",
            userpwdclass: "error",

            usertel: "",
            usertelreg: /^1[3-9][0-9]{9}$/,
            userteltip: "请输入常用的手机号",
            usertelclass: "error",

        },
        methods: {
            checkAttr(attr, tip) {
                if (this[attr + 'reg'].test(this[attr])) {
                    this[attr + "tip"] = "√";
                    this[attr + "class"] = "success";
                } else {
                    this[attr + "tip"] = tip;
                    this[attr + "class"] = "error";
                }
            }

        },
        watch: {
            username() {
                this.checkAttr("username", "请输出数字字母_$组成并且不能以数字开头长度6到10位")

            },
            userpwd() {
                this.checkAttr("userpwd", "密码数字字母组成长度6到10位")
            },
            usertel() {
                this.checkAttr("usertel", "请输入常用的手机号");
            }
        }
    })


</script>

</html>